package com.fs.system.college_admin.service;

import com.fs.system.college_admin.domain.CollegeAdmin;
import com.fs.system.college_admin.mapper.ActiveTypeMapper;
import com.fs.system.exception.CustomerException;
import com.fs.system.student.domain.ActType;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@Service
public class ActiveTypeService {

    @Resource
    private ActiveTypeMapper activeTypeMapper;

    public void addActiveType(ActType actType) {
        ActType dbActType = activeTypeMapper.selectActiveTypeByName(actType.getName());
        if(Objects.nonNull(dbActType)){
            throw new CustomerException("500","新增失败！分类名称已存在");
        }
        actType.setDeleted(false);
        actType.setCreateTime(LocalDateTime.now());
        activeTypeMapper.addOne(actType);
    }

    public List<ActType> selectActiveTypeList(ActType actType) {
        return activeTypeMapper.selectActiveTypeList(actType);
    }

    public PageInfo<ActType> selectPage(ActType actType, Integer pageNum, Integer pageSize) {
        //开启分页查询（传参：当前页码、每页分页个数）
        PageHelper.startPage(pageNum,pageSize);
        //查询所有的数据
        List<ActType> activeTypeList = activeTypeMapper.selectActiveTypeList(actType);
        //PageInfo执行分页操作
        return PageInfo.of(activeTypeList);
    }

    public void updateActiveType(ActType actType) {
        ActType dbActType = activeTypeMapper.selectActiveTypeByName(actType.getName());
        if(Objects.nonNull(dbActType)){
            throw new CustomerException("500","更新失败！分类名称已存在");
        }
        actType.setUpdateTime(LocalDateTime.now());
        activeTypeMapper.updateActiveType(actType);
    }

    /**
     * 单个删除
     * @param typeId：需修改的id
     * @param updateBy：修改操作的人
     */
    public void deleteActiveType(Long typeId, String updateBy) {
        ActType actType = activeTypeMapper.selectActiveTypeById(typeId);
        if(Objects.isNull(actType)){
            throw new CustomerException("500","删除失败");
        }
        actType.setDeleted(true);
        actType.setUpdateBy(updateBy);
        actType.setUpdateTime(LocalDateTime.now());
        activeTypeMapper.updateActiveType(actType);
    }

    /**
     * 批量删除
     * @param updateBy
     * @param ids
     */
    public void deleteBatch(List<Long> ids,String updateBy) {
        for (Long typeId :ids) {
            this.deleteActiveType(typeId, updateBy);
        }
    }
}
